package com.common.app.utls.network.interceptor

import com.common.app.helper.UserHelper
import com.common.app.utls.Constants
import com.common.app.utls.TimeUtils
import com.common.base.utils.JsonUtils
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import java.io.IOException

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：18/6/2025
 *
 * 描述：统一头部信息拦截器
 *
 * 修订历史：
 *
 */
class HeadUrlInterceptor : Interceptor {
    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val request: Request = chain.request()
        val builder: Request.Builder = request.newBuilder()

        builder.addHeader("Content-Type", "application/json;charset=UTF-8")
            .addHeader("Carrier", "-1")
            .addHeader("BsAppID", Constants.APP_ID)
            .addHeader("system_type", "1")
            .addHeader("system_version", "android")
            .addHeader("Lang", "zh-Hant")
            .addHeader("timezone", TimeUtils.getTimeZone())

        HeadersInterceptorsConfig.getInterceptorsConFig().forEach { (key, value) ->
            builder.addHeader(key, value)
        }

        if (UserHelper.getUser() == null){
            builder.removeHeader("UserId")
            builder.removeHeader("Authorization")
            builder.removeHeader("Token")
        }else{
            builder.addHeader("UserId", UserHelper.getUser()?.getId()?:"")
////                .addHeader("Token", UserHelper.getToken())
                .addHeader("Authorization", UserHelper.getToken())
//                .addHeader("Authorization", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3ODc3Mjc0MTgsImlhdCI6MTc1NjE5MTQxOCwidW5pb25JZCI6IiIsInVzZXJJZCI6NTN9.7xiBGmtHAKi5nyKZKRoBalpFOrnHxSxBUBykDTRvMXQ")
        }
        builder.addHeader("App-Client", JsonUtils.toJson(builder.build().headers))
        return chain.proceed(builder.build())
    }
}